package com.dingpeikeji.quickdeer.core.modules.orders.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.dingpeikeji.quickdeer.core.common.config.Global;
import com.dingpeikeji.quickdeer.core.common.persistence.Page;
import com.dingpeikeji.quickdeer.core.common.utils.StringUtils;
import com.dingpeikeji.quickdeer.core.common.web.BaseController;
import com.dingpeikeji.quickdeer.core.modules.orders.entity.TOrdersPerson;
import com.dingpeikeji.quickdeer.core.modules.orders.service.TOrdersPersonService;

/**
 * 实名认证Controller
 * @author gaozj
 * @version 2016-11-23
 */
@Controller
@RequestMapping(value = "${adminPath}/orders/tOrdersPerson")
public class TOrdersPersonController extends BaseController {

	@Autowired
	private TOrdersPersonService tOrdersPersonService;
	
	@ModelAttribute
	public TOrdersPerson get(@RequestParam(required=false) String id) {
		TOrdersPerson entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = tOrdersPersonService.get(id);
		}
		if (entity == null){
			entity = new TOrdersPerson();
		}
		return entity;
	}
	
	@RequiresPermissions("orders:tOrdersPerson:view")
	@RequestMapping(value = {"list", ""})
	public String list(TOrdersPerson tOrdersPerson, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<TOrdersPerson> page = tOrdersPersonService.findPage(new Page<TOrdersPerson>(request, response), tOrdersPerson); 
		model.addAttribute("page", page);
		return "modules/orders/tOrdersPersonList";
	}

	@RequiresPermissions("orders:tOrdersPerson:view")
	@RequestMapping(value = "form")
	public String form(TOrdersPerson tOrdersPerson, Model model) {
		model.addAttribute("tOrdersPerson", tOrdersPerson);
		return "modules/orders/tOrdersPersonForm";
	}

	@RequiresPermissions("orders:tOrdersPerson:edit")
	@RequestMapping(value = "save")
	public String save(TOrdersPerson tOrdersPerson, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, tOrdersPerson)){
			return form(tOrdersPerson, model);
		}
		tOrdersPersonService.save(tOrdersPerson);
		addMessage(redirectAttributes, "保存实名认证成功");
		return "redirect:"+Global.getAdminPath()+"/orders/tOrdersPerson/?repage";
	}
	
	@RequiresPermissions("orders:tOrdersPerson:edit")
	@RequestMapping(value = "delete")
	public String delete(TOrdersPerson tOrdersPerson, RedirectAttributes redirectAttributes) {
		tOrdersPersonService.delete(tOrdersPerson);
		addMessage(redirectAttributes, "删除实名认证成功");
		return "redirect:"+Global.getAdminPath()+"/orders/tOrdersPerson/?repage";
	}

}